import { observable, action } from 'mobx'
import * as api from 'lib/constants/api'
import { post } from 'lib/net'
import Toast from 'components/toast'

// class AddressModel {
//   constructor (data) {
//     if (data) {
//     }
//   }
// }

export default class BindBankCardVM {
  constructor (props) {
    this.props = props
    this.PageInitialization()
  }

  @observable test = ''

  // 银行卡卡号
  @observable bankCard = ''

  // 银行卡姓名
  @observable userName = ''

  // 获取银行卡信息
  @action onChange = (v, key) => {
    switch (key) {
      case 'BankCard':
        this.bankCard = v
        break
      case 'UserName':
        this.userName = v
        break
    }
  }

  // 去除字符串中的空格
  @action myTrim = (str, global) => {
    let result
    result = str.toString().replace(/(^\s+)|(\s+$)/g, '')
    if (global.toLowerCase() === 'g') {
      result = result.replace(/\s/g, '')
    }
    return result
  }

  // 保存银行卡信息
  @action handleBindBackCard = async (BankCode, BankIcon, BankName) => {
    if (BankName === undefined) {
      Toast.fail('请选择开户行')
    } else if (this.bankCard === '') {
      Toast.fail('请输入银行卡号')
    } else if (this.userName === '') {
      Toast.fail('请输入持卡人姓名')
    } else {
      let params = {
        WZId: window.localStorage.getItem('UserId'),
        BankNum: this.myTrim(this.bankCard, 'g'),
        BankUser: this.userName,
        BankName: BankName,
        BankIcon: BankIcon,
        BankCode: BankCode
      }
      console.log(params)
      let response = await post(api.WZBINDBANK, params)
      if (response.Success) {
        this.props.history.push('/withdraw')
      } else {
        Toast.fail(response.Message)
      }
    }
  }

  // 选择开户行
  @action handleChooseBank = (v) => {
    this.props.history.push('/bankList')
  }

  @action PageInitialization = async () => {
  }
}
